﻿/// <reference path="../../../Common/JQ/jquery-1.10.1.min.js" />
/// <reference path="../../../Common/MyJS/MyRemoteCall.js" />
/// <reference path="../../../Common/MyJS/MyAjax.js" />

//Ext.onReady(function () {
//    //GetLog(); 
//    SetOnContextMenu();
//    window.status = $$("txt_Title").value;
//    document.title = $$("txt_Title").value;
//    //设置位置
//    SetBtnsPosition();
//    setInterval(GetLog, 500);
//});

//标志用户是否正在黏贴，如果在黏贴的话，不要获取数据，否则影响黏贴操作
var g_IsPasting = false;

jQuery(document).ready(function () {
    //GetLog(); 
    SetOnContextMenu();
    window.status = $$("txt_Title").value;
    document.title = $$("txt_Title").value;
    //设置位置
    window.onresize = function () {
        SetBtnsPosition();
    };
    //document.body.onmousedown = function () {
    //    g_IsPasting = true;
    //};
    //document.body.onmouseup = function () {
    //    g_IsPasting = false;
    //};
    SetBtnsPosition();
    setInterval(GetLog, 1000);
});

//停止获取日志
function StopGetLog() {
    g_IsPasting = !g_IsPasting;
    if (g_IsPasting) {
        $$("btn_stopGetLog").value = "开始获取日志";
    }
    else {
        $$("btn_stopGetLog").value = "停止获取日志";
    }
};

//设置按钮的位置
function SetBtnsPosition() {
    //$$("div_Btns").style.position = "absolute";
    //document.body.clientHeight; //可见高度
    //document.body.scrollHeight; //文档的内容总高度
    $$("div_Btns").style.top = document.body.clientHeight + document.body.scrollTop - $$("div_Btns").offsetHeight;
};

//设置页面的右键属性
function SetOnContextMenu() {
    jQuery(document.body).contextmenu({
        items: [{
            text: '复制文本',
            action: function () {
                var sMsg = document.selection.createRange().text;
                if (sMsg) {
                    window.clipboardData.setData("Text", sMsg);
                }
            }
        }, {
            text: '刷新页面',
            action: function () {
                ReloadPage();
            }
        }, {
            text: '清空日志文件',
            action: function (target) {
                ClearLog();
            }
        }],
        width: "150px"
    });
    //var menu = new Ext.menu.Menu();
    //menu.add({ id: 'menu2', text: '刷新页面', handler: ReloadPage });
    //menu.add({ id: 'menu1', text: '清空日志文件', handler: ClearLog });

    ////禁止整个页面的右键
    //Ext.getDoc().on("contextmenu", function (e) {
    //    e.stopEvent();
    //});
    ////注册右键
    //Ext.getBody().on("contextmenu", function (e) {
    //    //alert(e.getPoint());
    //    menu.showAt(e.getXY()); //显示在当前位置 
    //});
};

function ReloadPage() {
    window.location.reload();
};

//清空日志
function ClearLog() {
    var sUrl =  "ClearLogs.aspx";
    var sResult = MyAjax.get(sUrl, { "logPath": $$("txt_LogPath").value });
    SetLogToPage(sResult);
    
    //Ext.Ajax.request({
    //    url: "ClearLogs.aspx",
    //    method: 'GET',
    //    params: { "logPath": $$("txt_LogPath").value },
    //    success: function (response, options) {
    //        //Ext.getBody().dom.innerText += response.responseText;
    //        SetLogToPage(response.responseText);
    //    },
    //    failure: function (response, options) {
    //        SetLogToPage('获取日志出错，错误编号：' + response.status);
    //    }
    //});
};

//获取最新的日志
function GetLog() {
    if (!g_IsPasting) {
        var sUrl = "GetLogs.aspx";
        var sResult = MyAjax.get(sUrl, { "logPath": $$("txt_LogPath").value });
        if (!g_IsPasting) SetLogToPage(sResult);
    }
    
    //Ext.Ajax.request({
    //    url: "GetLogs.aspx",
    //    method: 'POST',
    //    params: { "logPath": $$("txt_LogPath").value },
    //    success: function (response, options) {
    //        //Ext.getBody().dom.innerText += response.responseText;
    //        SetLogToPage(response.responseText);
    //    },
    //    failure: function (response, options) {
    //        //Ext.MessageBox.alert('失败', '获取日志出错，错误编号：' + response.status);
    //        SetLogToPage('获取日志出错，错误编号：' + response.status);
    //    }
    //});
};

//把日志显示到页面上
function SetLogToPage(sLog) {
    $$("Div_Log").innerText = sLog;
    if (!g_stopAutoScroll) {
        var iHeight = $$("Div_Log").scrollHeight;
        window.scrollTo(0, iHeight);
    }
    //Ext.get("Div_Log").dom.innerText = sLog;
    //if (!g_stopAutoScroll) {
    //    Ext.getBody().scroll("down", Ext.get("Div_Log").getHeight(), false);
    //}
};

//停止日志自动滚动
var g_stopAutoScroll = false;
function StopAutoScroll() {
    if (g_stopAutoScroll) {
        g_stopAutoScroll = false;
        $$("btn_stopScroll").value = "停止自动滚动页面";
    }
    else {
        g_stopAutoScroll = true;
        $$("btn_stopScroll").value = "开始自动滚动页面";
    }
};

//打开日志目录
function OpenLogPath() {
    var sUrl = "OpenLogPath.aspx";
    MyAjax.get(sUrl, { "logPath": $$("txt_LogPath").value });
    //Ext.Ajax.request({
    //    url: "OpenLogPath.aspx",
    //    method: 'GET',
    //    params: { "logPath": $$("txt_LogPath").value },
    //    success: function (response, options) {
    //        //Ext.getBody().dom.innerText += response.responseText;
    //        //SetLogToPage(response.responseText);
    //    },
    //    failure: function (response, options) {
    //        //Ext.MessageBox.alert('失败', '获取日志出错，错误编号：' + response.status);
    //        //SetLogToPage('获取日志出错，错误编号：' + response.status);
    //    }
    //});
};